package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsWasteCountBo;
import com.kxmall.huishou.domain.vo.HsWasteCountVo;
import com.kxmall.huishou.domain.HsWasteCount;
import com.kxmall.huishou.mapper.HsWasteCountMapper;
import com.kxmall.huishou.service.IHsWasteCountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 废品回收统计Service业务层处理
 *
 * @author kxmall
 * @date 2024-05-13
 */
@RequiredArgsConstructor
@Service
public class HsWasteCountServiceImpl implements IHsWasteCountService {

    private final HsWasteCountMapper baseMapper;

    /**
     * 查询废品回收统计
     */
    @Override
    public HsWasteCountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询废品回收统计列表
     */
    @Override
    public TableDataInfo<HsWasteCountVo> queryPageList(HsWasteCountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWasteCount> lqw = buildQueryWrapper(bo);
        Page<HsWasteCountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询废品回收统计列表
     */
    @Override
    public List<HsWasteCountVo> queryList(HsWasteCountBo bo) {
        LambdaQueryWrapper<HsWasteCount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWasteCount> buildQueryWrapper(HsWasteCountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWasteCount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeDevice() != null, HsWasteCount::getTypeDevice, bo.getTypeDevice());
        lqw.eq(bo.getGreenWeb() != null, HsWasteCount::getGreenWeb, bo.getGreenWeb());
        lqw.eq(bo.getGreenCore() != null, HsWasteCount::getGreenCore, bo.getGreenCore());
        lqw.eq(bo.getCarbonEmissionTotal() != null, HsWasteCount::getCarbonEmissionTotal, bo.getCarbonEmissionTotal());
        lqw.eq(bo.getCarbonEmissionIntensity() != null, HsWasteCount::getCarbonEmissionIntensity, bo.getCarbonEmissionIntensity());
        lqw.eq(bo.getWasteTotal() != null, HsWasteCount::getWasteTotal, bo.getWasteTotal());
        lqw.eq(bo.getWasteTotalMonth() != null, HsWasteCount::getWasteTotalMonth, bo.getWasteTotalMonth());
        lqw.eq(bo.getWasteAmount() != null, HsWasteCount::getWasteAmount, bo.getWasteAmount());
        return lqw;
    }

    /**
     * 新增废品回收统计
     */
    @Override
    public Boolean insertByBo(HsWasteCountBo bo) {
        HsWasteCount add = BeanUtil.toBean(bo, HsWasteCount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改废品回收统计
     */
    @Override
    public Boolean updateByBo(HsWasteCountBo bo) {
        HsWasteCount update = BeanUtil.toBean(bo, HsWasteCount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWasteCount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除废品回收统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
